# _*_ coding: utf-8 _*_
import os, sys
import uuid, time
import pytesseract
from selenium import webdriver
from ctypes import windll
from PIL import Image


def tesseract_file(file_path):
        #my_env = {'TESSDATA_PREFIX': r'C:\Program Files\Tesseract-OCR\testdata'}
        image = Image.open(os.path.realpath(file_path))
        result = pytesseract.image_to_string(image)
        return result

def login_abc(param):
        url = 'https://perbank.abchina.com/EbankSite/startup.do'
        ie_driver = 'C:\Program Files\Internet Explorer\IEDriverServer.exe'
        #os.environ["webdriver.ie.driver"] = ie_driver
        browser = webdriver.Ie(ie_driver)
        browser.maximize_window()
        browser.get(url)
        time.sleep(1)

        browser.find_element_by_id('username').send_keys(param['username'])

        #载入DD
        libpath = 'libs/DD81200x32.dll' if sys.platform == 'win32' else 'libs/DD81200x64.32.dll'
        dd_dll = windll.LoadLibrary(libpath)

        vk = {'5': 205, 'c': 503, 'n': 506, 'z': 501, '3': 203, '1': 201, 'd': 403, '0': 210, 'l': 409, '8': 208,
              'w': 302,
              'u': 307, '4': 204, 'e': 303, '[': 311, 'f': 404, 'y': 306, 'x': 502, 'g': 405, 'v': 504, 'r': 304,
              'i': 308,
              'a': 401, 'm': 507, 'h': 406, '.': 509, ',': 508, ']': 312, '/': 510, '6': 206, '2': 202, 'b': 505,
              'k': 408,
              '7': 207, 'q': 301, "'": 411, '\\': 313, 'j': 407, '`': 200, '9': 209, 'p': 310, 'o': 309, 't': 305,
              '-': 211,
              '=': 212, 's': 402, ';': 410}
        #需要组合shift的按键。
        vk2 = {'"': "'", '#': '3', ')': '0', '^': '6', '?': '/', '>': '.', '<': ',', '+': '=', '*': '8', '&': '7',
               '{': '[', '_': '-',
               '|': '\\', '~': '`', ':': ';', '$': '4', '}': ']', '%': '5', '@': '2', '!': '1', '(': '9'}

        def down_up(code):
            dd_dll.DD_key(vk[code], 1)
            dd_dll.DD_key(vk[code], 2)

        def dd(key):
            if i.isupper():
                # 按下抬起 500是shift键码
                dd_dll.DD_key(500, 1)
                down_up(i.lower())
                dd_dll.DD_key(500, 2)

            elif i in '~!@#$%^&*()_+{}|:"<>?':
                dd_dll.DD_key(500, 1)
                down_up(vk2[i])
                dd_dll.DD_key(500, 2)
            else:
                down_up(i)

        img = browser.find_element_by_id('vCode')
        x, y = img.location['x'] + 50, img.location['y'] + 50

        #移动到密码框点击激活
        dd_dll.DD_mov(x, y)
        dd_dll.DD_btn(1)
        time.sleep(1)

        #依次输入密码字符
        for i in param['password']:
            dd(i)
            time.sleep(0.2)

        left, top = img.location['x'], img.location['y']
        width, height = img.size['width'] + left, img.size['height'] + top
        os.makedirs('imgs')
        oldpath = 'imgs/' + str(uuid.uuid1()) + '.png'
        browser.save_screenshot(oldpath)
        oldpic = Image.open(oldpath)
        newpic = oldpic.crop((left, top, width, height))
        picpath = 'imgs/' + str(uuid.uuid1()) + '.png'
        newpic.save(picpath)
        os.remove(oldpath)
        
        #tesseract可以识别简单的图形验证码，最后都要自己训练
        vcode = tesseract_file(picpath)
        browser.find_element_by_name('code').send_keys(vcode)

        #os.remove(picpath)
        browser.find_element_by_id('logo').click()

        time.sleep(1)
        logerr = browser.find_element_by_class_name('logon-error').text
        print(logerr)

if __name__ == '__main__':
    login_abc({'username':'13311111111', 'password':'123456'})